﻿using System.Web;
using System;
using System.Data;
using System.Linq;

using VIT.DataHelper;
using VIT.DataHelper.DataAccess;
using VIT.Library.Web;

namespace VIT.Web.Admin.UserControl
{
    public partial class SideBar : System.Web.UI.UserControl
    {
        private string[] lstRole;
        protected void Page_Load(object sender, EventArgs e)
        {
            string Roles = Session["userRoles"].ToString();
            lstRole = Roles.Split('|');

            string strQuery = string.Empty;
            strQuery = "select * from Menu where Publish = 1";
            SQLSupport sqlsp = new SQLSupport();
            DataTable table = sqlsp.ExecuteDataset(false, strQuery).Tables[0];

            lblScript.Text = GenTag(table);
            //lblScript.Text = "<script language='javascript' type='text/javascript'>";
            //string Roles = Session["userRoles"].ToString();
            //string cli;
            //foreach (string role in Roles.Split('|'))
            //{
            //    if(role.Contains("."))
            //    {
            //        cli = role.Split('.')[0];
            //        if (!cli.Contains("Edit"))
            //        {
            //            lblScript.Text += "try{document.getElementById('" + cli + "').style.display = 'block';}catch (err){}";
            //        }
            //    }
            //}
            //lblScript.Text += "</script>";
        }

        //<ul>
        //    <li id="CategoryList" style="display:none"><a href="<%= URL.linkComponent("Category", "CategoryList")%>">Quản lý danh mục</a></li>
        //</ul>

        public string GenTag(DataTable table, string parentId = "0")
        {
            string list_menu;
            DataRow[] subcats = table.Select("ParentId = '" + parentId + "'", "Orders");
            if (subcats.Length == 0) return null;
            list_menu = "<ul>";
            foreach (DataRow subcat in subcats)
            {
                string res = string.Empty;

                res = GenTag(table, subcat["Id"].ToString());
                string link = subcat["Link"].ToString().Length > 0 ? subcat["Link"].ToString() : subcat["ViewName"].ToString().Length > 0 ? URL.linkComponent(subcat["ComponentName"].ToString(), subcat["ViewName"].ToString()) : "#";
                if (link != "#")
                {
                    string[] pages = link.Split('/');
                    string page = pages[pages.Length - 1];
                    if (lstRole.Any(o => o.ToUpper() == page.ToUpper()))
                        list_menu += string.Format("<li id='{0}'><a href='{1}'>{2}</a></li>", subcat["Description"], link, subcat["Text"]);
                }

                if (res != null)
                {
                    if (res.Length > 10)
                        list_menu += string.Format("<li id='{0}'><a href='#'>{1}</a></li>", subcat["Description"], subcat["Text"]);
                    list_menu += res;
                }
            }
            list_menu += "</ul>";
            return list_menu;
        }
    }
}